<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */



class Admin_StatisticsController extends Zend_Controller_Action
{
	private $_statisticsService; 
	
    public function init()
    {
		$this->_statisticsService = $this->_helper->getService('Core_Service_Statistics');
    }

    public function indexAction()
    {
    	
    }
    
    public function generalAction()
    {
        $this->_helper->acl()->isAllowed('service:statistics', 'fetch');

		$filterForm = new Admin_Form_Statistics_GeneralFilter();
		
        $request = $this->getRequest();
    	if ($request->isGet() && $filterForm->isValid($request->getQuery()))
    	{
    		$filterData = $this->_getGetDataOnlyNotNulls();
    		
    	} else
    	{
    		$filterData = array();
    	}	
    	$filterForm->fillForm($filterData);
    	$totalHoursAndShifts = $this->_statisticsService->fetchTotalHoursAndShifts($filterData);

    	$this->view->filterForm = $filterForm;
    	$this->view->totalHoursAndShifts = $totalHoursAndShifts;
    }
    
    public function byMemberAction()
    {
        $this->_helper->acl()->isAllowed('service:statistics', 'fetch');

    	$filterForm = new Admin_Form_Statistics_ByMemberFilter();
        
    	$request = $this->getRequest();
    	if ($request->isGet() && $filterForm->isValid($request->getQuery()))
    	{
    		$filterData = $this->_getGetDataOnlyNotNulls();
    	} else {
    		$filterData = array();
    	}
		$filterForm->fillForm($filterData);
    	
		$hoursAndShifts = $this->_statisticsService->fetchHoursAndShiftsPerMember($filterData);

		$this->view->hoursAndShifts = $hoursAndShifts;
	   	$this->view->filterForm = $filterForm;
    }
        
    public function inactiveMembersAction()
    {
        $this->_helper->acl()->isAllowed('service:statistics', 'fetch');

    	$filterForm = new Admin_Form_Statistics_InactiveMembersFilter();
        
    	$request = $this->getRequest();
    	if ($request->isGet() && $filterForm->isValid($request->getQuery()))
    	{
    		$filterData = $this->_getGetDataOnlyNotNulls();
    	} else {
    		$filterData = array();
    	}
		$filterForm->fillForm($filterData);
    	
		$inactiveMembers = $this->_statisticsService->fetchInactiveMembers($filterData);

		$this->view->hoursAndShifts = $inactiveMembers;
	   	$this->view->filterForm = $filterForm;
    }
    
    private function _getGetDataOnlyNotNulls()
    {
    	$keyValues = array();
    	foreach ($this->_getAllParams() as $key => $value)
    		if ($this->_getParam($key,false))
    			$keyValues[$key] = $value;
    	return $keyValues;
    }  
}